package com.cyl.order.controller;

import java.io.IOException;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.cyl.order.domain.TOrderInfo;
import com.cyl.order.service.ITOrderInfoService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 订单信息管理Controller
 * 
 * @author ruoyi
 * @date 2025-04-14
 */
@RestController
@RequestMapping("/order/orderInfo")
public class TOrderInfoController extends BaseController
{
    @Autowired
    private ITOrderInfoService tOrderInfoService;

    /**
     * 查询订单信息管理列表
     */
    @PreAuthorize("@ss.hasPermi('order:orderInfo:list')")
    @GetMapping("/list")
    public TableDataInfo list(TOrderInfo tOrderInfo)
    {
        startPage();
        List<TOrderInfo> list = tOrderInfoService.selectTOrderInfoList(tOrderInfo);
        return getDataTable(list);
    }

    /**
     * 导出订单信息管理列表
     */
    @PreAuthorize("@ss.hasPermi('order:orderInfo:export')")
    @Log(title = "订单信息管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TOrderInfo tOrderInfo)
    {
        List<TOrderInfo> list = tOrderInfoService.selectTOrderInfoList(tOrderInfo);
        ExcelUtil<TOrderInfo> util = new ExcelUtil<TOrderInfo>(TOrderInfo.class);
        try {
            util.exportExcel(response, list, "订单信息管理数据");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取订单信息管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('order:orderInfo:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(tOrderInfoService.selectTOrderInfoById(id));
    }

    /**
     * 新增订单信息管理
     */
    @PreAuthorize("@ss.hasPermi('order:orderInfo:add')")
    @Log(title = "订单信息管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add( @RequestBody String rawJson)
    {
        System.out.println(rawJson);
        TOrderInfo dto = JSON.parseObject(rawJson, TOrderInfo.class);
        System.out.println(dto.toString());

        //return null;
        return toAjax(tOrderInfoService.insertTOrderInfo(dto));
    }

    /**
     * 修改订单信息管理
     */
    @PreAuthorize("@ss.hasPermi('order:orderInfo:edit')")
    @Log(title = "订单信息管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TOrderInfo tOrderInfo)
    {
        return toAjax(tOrderInfoService.updateTOrderInfo(tOrderInfo));
    }

    /**
     * 删除订单信息管理
     */
    @PreAuthorize("@ss.hasPermi('order:orderInfo:remove')")
    @Log(title = "订单信息管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tOrderInfoService.deleteTOrderInfoByIds(ids));
    }

    /**
     * 导出信息管理
     */
    @PreAuthorize("@ss.hasPermi('order:orderInfo:remove')")
    @Log(title = "导出信息管理", businessType = BusinessType.EXPORT)
    @PostMapping(value = "/orderExport/{id}")
    public void exportOrderInfo(@PathVariable Long id,HttpServletResponse response)
    {
        tOrderInfoService.exportOrderInfo(id,response);
    }

    /**
     * 获取代付金额
     */
    @Log(title = "获取代付金额", businessType = BusinessType.EXPORT)
    @GetMapping(value = "/queryAmount")
    public AjaxResult queryAmount()
    {
        return tOrderInfoService.queryAmount();
    }
}
